import pygame
import random


class Plane(pygame.sprite.Sprite):
    def __init__(self, image_path, x, y):
        super().__init__()
        self.image = pygame.image.load(image_path).convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y


class PlayerPlane(Plane):
    def __init__(self, screen):
        super().__init__("./feiji/hero.gif", 230, 600)
        self.screen = screen
        self.bullets = pygame.sprite.Group()
        self.is_hit = False

    def move_left(self):
        if self.rect.x > 0:
            self.rect.x -= 10

    def move_right(self):
        if self.rect.x < 480 - self.rect.width:
            self.rect.x += 10

    def launch_bullet(self):
        bullet = Bullet("./feiji/bullet-3.gif", self.rect.centerx, self.rect.y)
        self.bullets.add(bullet)

    def check_collision(self, enemies, scoreboard):
        for enemy in enemies:
            if pygame.sprite.collide_rect(self, enemy):
                self.is_hit = True
                break

        if self.is_hit:
            self.kill()
            scoreboard.update_score(-10)
            pygame.time.delay(500)

    def display(self):
        self.screen.blit(self.image, self.rect)
        self.bullets.update()
        self.bullets.draw(self.screen)

class EnemyPlane(Plane):
    def __init__(self, x, y):
        super().__init__("./feiji/enemy-1.gif", x, y)
        self.direction = random.choice(["left", "right"])
        self.speed = random.randint(1, 2)

    def move(self):
        if self.direction == "right":
            self.rect.x += self.speed
        elif self.direction == "left":
            self.rect.x -= self.speed

        if self.rect.x > 480 - self.rect.width:
            self.direction = "left"
        elif self.rect.x < 0:
            self.direction = "right"


class EnemyGroup(pygame.sprite.Group):
    def __init__(self, screen):
        super().__init__()
        self.screen = screen
        self.add_enemies()

    def add_enemies(self):
        for _ in range(6):
            x = random.randint(0, 480 - 50)
            y = random.randint(0, 200)
            enemy = EnemyPlane(x, y)
            self.add(enemy)

    def display(self):
        for enemy in self:
            self.screen.blit(enemy.image, enemy.rect)

    def move(self):
        for enemy in self:
            if isinstance(enemy, EnemyPlane):
                enemy.move()

    def launch_bullet(self):
        for enemy in self:
            if random.randint(1, 100) == 88:
                bullet = Bullet("./feiji/bullet-1.gif", enemy.rect.centerx, enemy.rect.y + enemy.rect.height)
                self.add(bullet)

    def check_collision(self, bullets, scoreboard, ):
        for enemy in self:
            for bullet in bullets:
                if pygame.sprite.collide_rect(enemy, bullet):
                    self.remove(enemy)
                    player_plane.bullets.remove(bullet)
                    scoreboard.update_score(1)
                    break


class Scoreboard:
    def __init__(self, screen):
        self.score = 0
        self.screen = screen

    def update_score(self, value):
        self.score += value

    def display(self):
        font = pygame.font.Font(None, 24)
        text = font.render("Score: {}".format(self.score), True, (255, 255, 255))
        self.screen.blit(text, (10, 10))


class Bullet(pygame.sprite.Sprite):
    def __init__(self, image_path, x, y):
        super().__init__()
        self.image = pygame.image.load(image_path).convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.x = x - self.rect.width // 2
        self.rect.y = y

    def update(self):
        self.rect.y -= 5
        if self.rect.y < -self.rect.height:
            self.kill()